package vip.xiaonuo.smzq.modular.psba.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.smzq.modular.deom.ToolData;
import vip.xiaonuo.smzq.modular.lifecycle.entity.KclKs;
import vip.xiaonuo.smzq.modular.psba.entity.*;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbaSqjlFyfVo;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbaSqjlJbqkVo;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbaSqjlVo;
import vip.xiaonuo.smzq.modular.psba.entity.vo.Fyfxxtb;
import vip.xiaonuo.smzq.modular.psba.mapper.*;
import vip.xiaonuo.smzq.modular.psba.service.IPsbaSqjlBgService;
import vip.xiaonuo.smzq.modular.psba.service.IPsbaSqjlDztjService;
import vip.xiaonuo.smzq.modular.psba.service.IPsbaSqjlJbqkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.smzq.modular.psba.service.IPsbaSqjlService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 评审备案申请记录-非压覆基本情况 服务实现类
 *
 * @author Kevin
 * @since 2024-06-11
 */
@Service
public class PsbaSqjlJbqkServiceImpl extends ServiceImpl<PsbaSqjlJbqkMapper, PsbaSqjlJbqk> implements IPsbaSqjlJbqkService {

    @Resource
    private PsbaSqjlJbqkMapper psbaSqjlJbqkMapper;
    @Resource
    private PsbaSqjlBgMapper psbaSqjlBgMapper;//申请-报告
    @Resource
    private PsbaSqjlDztjMapper psbaSqjlDztjMapper;//申请-地质条件
    //压覆
    @Autowired
    private PsbaGtYfjbqkMapper psbaGtYfjbqkMapper;
    //非压覆
    @Autowired
    private PsbaGtJbqkMapper psbaGtJbqkMapper;


    @Resource
    private IPsbaSqjlBgService bgService;

    @Resource
    private IPsbaSqjlDztjService dztjService;

    @Resource
    private IPsbaSqjlService psbaSqjlService;

    /**
     * 添加评审备案申请记录-非压覆基本情况
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(PsbaSqjlFyfVo psbaSqjlFyfVo) {
        if (psbaSqjlFyfVo == null) {
            throw new RuntimeException("数据为空");
        }
        //生成评审备案编号
        String nd = DateUtil.format(new Date(), "YYYY");
        String sxh = psbaGtJbqkMapper.getSxh(nd);

        //存储非压覆信息
//        psbaSqjlFyfVo.getPsbaSqjlJbqk().setPsbabh(psbabh);
//        psbaSqjlFyfVo.getPsbaSqjlBg().setPsbabh(psbabh);
//        psbaSqjlFyfVo.getPsbaSqjlDztj().setPsbabh(psbabh);
        this.save(psbaSqjlFyfVo.getPsbaSqjlJbqk());
        bgService.save(psbaSqjlFyfVo.getPsbaSqjlBg());
        dztjService.save(psbaSqjlFyfVo.getPsbaSqjlDztj());
    }

    @Override
    public Page<PsbaSqjlJbqk> page(PsbaSqjlJbqk psbaSqjlJbqk) {
        QueryWrapper<PsbaSqjlJbqk> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public Fyfxxtb getFyfxxtb(String psbabh) {
        Fyfxxtb fyfxxtb=new Fyfxxtb();
        //申请报告
        QueryWrapper<PsbaSqjlBg> queryWrapperbg = new QueryWrapper<>();
        queryWrapperbg.eq("psbabh",psbabh);
        PsbaSqjlBg psbaSqjlBg = psbaSqjlBgMapper.selectOne(queryWrapperbg);
        if (psbaSqjlBg!=null){
            fyfxxtb.setPsbaSqjlBg(psbaSqjlBg);
        }
        //申请地质条件
        QueryWrapper<PsbaSqjlDztj> queryWrapperdztj = new QueryWrapper<>();
        queryWrapperdztj.eq("psbabh",psbabh);
        PsbaSqjlDztj psbaSqjlDztj = psbaSqjlDztjMapper.selectOne(queryWrapperdztj);
        if (psbaSqjlDztj!=null){
            fyfxxtb.setPsbaSqjlDztj(psbaSqjlDztj);
        }
        //申请基本信息
//        QueryWrapper<PsbaSqjlJbqk> queryWrapperfyfxq = new QueryWrapper<>();
//        queryWrapperfyfxq.eq("psbabh",psbabh);
//        PsbaSqjlJbqk psbaSqjlJbqk = psbaSqjlJbqkMapper.selectOne(queryWrapperfyfxq);
//        if (psbaSqjlJbqk!=null){
//            fyfxxtb.setPsbaSqjlJbqk(psbaSqjlJbqk);
//        }

        //关联查询文件
        MPJLambdaWrapper<PsbaSqjlJbqk> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(PsbaSqjlJbqk.class)
                .selectAs(GtWjglb::getWjmc, PsbaSqjlJbqkVo::getBgwjmc)
                .leftJoin(GtWjglb.class, GtWjglb::getPkid, PsbaSqjlJbqk::getBgwj)
                .eq(PsbaSqjlJbqk::getPsbabh, psbabh);
        List<PsbaSqjlJbqkVo> list = psbaSqjlJbqkMapper.selectJoinList(PsbaSqjlJbqkVo.class, wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            PsbaSqjlJbqkVo jbqk = new PsbaSqjlJbqkVo();
            BeanUtil.copyProperties(list.get(0), jbqk);
            fyfxxtb.setPsbaSqjlJbqk(jbqk);
        }

        return fyfxxtb;
    }
}
